package com.baiytfp.hf.HFDataExchange.action.controller.view;

import com.baiytfp.hf.baiytfp.common.util.StringUtil;
import com.baiytfp.hf.baiytfp.framework.base.BaseController;
import com.baiytfp.hf.baiytfp.hfutils.HFDBHelper;
import com.baiytfp.hf.baiytfp.hfutils.HFRequest;
import com.baiytfp.hf.HFDataExchange.common.base.anotation.Auth;
import com.baiytfp.hf.HFDataExchange.common.model.base.TableName;
import com.baiytfp.hf.HFDataExchange.common.model.state.TagType;
import com.baiytfp.hf.HFDataExchange.common.model.table.*;
import com.baiytfp.hf.HFDataExchange.common.model.vo.BaseUserVO;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Controller
@RequestMapping("/view/user")
public class UserVController extends BaseController {

	@RequestMapping("/manage")
	@Auth(checkMenu=true)
	public ModelAndView manage(HttpServletRequest request,HttpServletResponse response) throws Exception {

		ModelAndView mav = new ModelAndView();

		return mav;
	}

	@RequestMapping("/userlist")
	@Auth(checkMenu=true)
	public ModelAndView userlist(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String dept_id = HFRequest.getIstance().getString(request, "dept_id");
		String is_load_all = HFRequest.getIstance().getString(request, "is_load_all", "0");

		Dept deptVO = HFDBHelper.read(TableName.DEPT, " where tree_id=? ", Dept.class, dept_id);

		ModelAndView mav = new ModelAndView();
		if (is_load_all.equals("1")) {
			deptVO.setTree_id("");
		}
		mav.addObject("deptVO", deptVO);

		return mav;
	}

	@RequestMapping("/useredit")
	@Auth(checkMenu=true)
	public ModelAndView useredit(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String key = HFRequest.getIstance().getString(request, "itemid");

		//所有角色
		List<Role> list_role = HFDBHelper.list(TableName.ROLE, " order by order_num asc ", Role.class, null);

		//相关岗位
		List<Position> list_position = HFDBHelper.list(TableName.POSITION, " order by id asc", Position.class, null);

		//相关标签
		List<Tags> list_tags = HFDBHelper.list(TableName.TAGS, " where tag_type=? order by id asc", Tags.class, new Object[]{TagType.USER.getKey()});

		if(!"".equals(key) && !"0".equals(key)){	//查看
			User_account userAccountVO = HFDBHelper.read(TableName.USER_ACCOUNT, " where user_code=? ", User_account.class, key);
			User_info userInfoVO = HFDBHelper.read(TableName.USER_INFO, " where user_code=? ", User_info.class, key);

			User_dept userDeptVO = HFDBHelper.read(TableName.USER_DEPT, " where user_code=? and is_current=1 ", User_dept.class, userAccountVO.getUser_code());
			Dept deptVO = HFDBHelper.read(TableName.DEPT, " where tree_id=? ", Dept.class, userDeptVO.getDept_id());
			DetectGroup detectGroupVO = HFDBHelper.read("hfdatastd_detect_group"," where id=? ", DetectGroup.class, userDeptVO.getGroup_id());

			//已有角色
			List<User_role> list_user_role = HFDBHelper.list(TableName.USER_ROLE, " where user_code=? ", User_role.class, new Object[]{userAccountVO.getUser_code()});

			//已有岗位
			List<User_position> list_user_position = HFDBHelper.list(TableName.USER_POSITION, " where user_code=? ", User_position.class, new Object[]{userAccountVO.getUser_code()});

			ModelAndView mav = new ModelAndView();
			mav.addObject("userAccountVO", userAccountVO);
			mav.addObject("userInfoVO", userInfoVO);

			mav.addObject("deptVO", deptVO);
			mav.addObject("detectGroupVO", detectGroupVO);

			mav.addObject("is_read", HFRequest.getIstance().getString(request, "is_read", ""));

			mav.addObject("list_role", list_role);
			mav.addObject("list_user_role", list_user_role);

			mav.addObject("list_position", list_position);
			mav.addObject("list_user_position", list_user_position);

			mav.addObject("list_tags", list_tags);

			return mav;

		}else{	//新增

			String dept_id = HFRequest.getIstance().getString(request, "dept_id");
			Dept deptVO = HFDBHelper.read(TableName.DEPT, " where tree_id=? ", Dept.class, dept_id);



			ModelAndView mav = new ModelAndView();

			mav.addObject("userAccountVO", null);
			mav.addObject("userInfoVO", null);

			mav.addObject("deptVO", deptVO);

			mav.addObject("list_role", list_role);

			mav.addObject("list_position", list_position);

			mav.addObject("list_tags", list_tags);

			return mav;
		}
	}
	
	@RequestMapping("/userselect")
	public ModelAndView userselect(HttpServletRequest request,HttpServletResponse response) throws Exception {

		ModelAndView mav = new ModelAndView();
		String dept_id = HFRequest.getIstance().getString(request, "dept_id");
		mav.addObject("dept_id", dept_id);	//传入的部门搜索条件
		mav.addObject("is_single", HFRequest.getIstance().getString(request, "is_single", "true"));
		
		return mav;
	}
	
}
